(function ($) {
	$.util.namespace("loginpage");
	
	var loginBtn = "#loginBtn", btnClick = null, showErrorMsg = "#showErrorMsg", validBox = "#validBox", validateCodeImg = "#validateCodeImg"
		iforgotValidateCodeImg = "#iforgotValidateCodeImg", showIforgotErrorMsg = "#showIforgotErrorMsg", iforgotClick = "#iforgotClick",
		countTime = null, login_box = ".login_box", login_qq = "#login_qq", login_weixin = "#login_weixin", login_weibo = "#login_weibo",
		iforgot_box = ".iforgot_box";
	
	//页面载入初始化
	window.loginpage.init = function() {
		$('#icheck input').iCheck({
			checkboxClass: 'icheckbox_minimal-blue'
	   	});
		
		$(validateCodeImg).click(function(){
			$(this).attr("src", $.ctx+"/validCodeServlet?d="+new Date().getTime());
		});
		$(iforgotValidateCodeImg).click(function(){
			$(this).attr("src", $.ctx+"/validCodeServlet?d="+new Date().getTime());
		});
		
		
		/**
		 * Velocity动画
		 */
		//注册动画
		$.Velocity.RegisterUI("fast.loginBoxIn", {
			defaultDuration: 500, 
			calls: [
			        [{opacity: [1, 0], rotateZ: 720, scale: [1, 0]},1]
			       ],
			reset: { rotateZ: 0 }
		});
		$.Velocity.RegisterUI("fast.loginBoxOut", {
			defaultDuration: 500, calls: [[{opacity: [0, 1], scale: [0, 1]}]]
		});
		$.Velocity.RegisterUI("fast.iforgotBoxIn", {
			defaultDuration: 500, calls: [[{opacity: [1, 0.8], scale: [1, 0], translateX: [0, 1000]},2]]
		});
		$.Velocity.RegisterUI("fast.iforgotBoxOut", {
			defaultDuration: 500, calls: [[{opacity: [0, 1], scale: [0, 1]}]]
		});
		$.Velocity.RegisterUI("fast.loginIconIn", {
			defaultDuration: 500, calls: [[{opacity: [1, 0], translateY: [0, 20]}]]
		})
		$.Velocity.RegisterUI("fast.loginIconOut", {
			defaultDuration: 500, calls: [[{opacity: [0, 1], translateY: [20, 0]}]]
		})
		//定义动画序列
		var seqLoginBoxIn = [{ e: $(login_box), p: "fast.loginBoxIn" }];
		var seqLoginBoxOut = [{ e: $(login_box), p: "fast.loginBoxOut" }];
		var seqIforgotBoxIn = [{ e: $(iforgot_box), p: "fast.iforgotBoxIn" }];
		var seqIforgotBoxOut = [{ e: $(iforgot_box), p: "fast.iforgotBoxOut" }];
		var seqLoginIconIn = [
		                      { e: $(login_qq), p: "fast.loginIconIn", o: { delay: 800, sequenceQueue: false } },
		                      { e: $(login_weixin), p: "fast.loginIconIn", o: { sequenceQueue: false } },
		                      { e: $(login_weibo), p: "fast.loginIconIn", o: { sequenceQueue: false } }
		                     ];
		var seqLoginIconOut = [
		                      { e: $(login_qq), p: "fast.loginIconOut", o: { delay: 800, sequenceQueue: false } },
		                      { e: $(login_weixin), p: "fast.loginIconOut", o: { sequenceQueue: false } },
		                      { e: $(login_weibo), p: "fast.loginIconOut", o: { sequenceQueue: false } }
		                     ];
		
		$("#fullpage").fullpage({
			anchors: ['login', 'iforgot', 'help'],
			slidesNavigation: true,
			navigation: false,
			menu: '#menu',
			afterRender: function(){},
			afterResize: function(){},
			afterLoad: function(anchorLink, index){ //滚动到某一section,且滚动结束后触发
				switch(index){
				case 1:
					$.Velocity.RunSequence(seqLoginBoxIn) ;
					$.Velocity.RunSequence(seqLoginIconIn) ;
					break;
				case 2:
					$.Velocity.RunSequence(seqIforgotBoxIn) ;
					break;
				case 3:
					break;
				default:
					break;
				}
			},
			onLeave: function(index, nextIndex, direction){ //在离开一个section时，会触发一次此回调函数
				switch(index){
				case 1:
					$.Velocity.RunSequence(seqLoginBoxOut) ;
					$.Velocity.RunSequence(seqLoginIconOut) ;
					break;
				case 2:
					$.Velocity.RunSequence(seqIforgotBoxOut) ;
					break;
				case 3:
					break;
				default:
					break;
				}
			},
			afterSlideLoad: function(anchorLink, index, slideAnchor, slideIndex){ //滚动某一幻灯片后触发
				
			},
			afterSlideLeave: function(anchorLink, index, slideIndex, direction, nextSlideIndex){ //离开幻灯片时触发
				
			}
		});
	};
	
	var keydown, loginEnter, loginClickBtn ;
	/**
	 * 回车登录事件
	 */
	window.loginpage.keydownLogin = function() {
		keydown = $(".login_box input, .login_box button").bind("keydown", loginEnter = function(e) {
			if (e.keyCode == 13) {
				validLogin();
			}
		});
	};
	
	/**
	 * 点击登录事件
	 */
	window.loginpage.clickLogin = function() {
		loginClickBtn = $(loginBtn).bind("click", btnClick = function() {
			
			validLogin();
		});
	};
	
	//客户的输入验证
	function validLogin() {
		var account = $("input[name=account]").val() ;
		var password = $("input[name=password]").val() ;
		var validateCode = $("input[name=validateCode]").val() ;
		var remember = $("input[name=remember]").is(":checked") ;
		
		if(account.trim().length == 0) {
			$(showErrorMsg).show().children("#msg").html("账号不能为空或者为空格!");
			return ;
		}
		/*if(password.trim().length == 0) {
			$(showErrorMsg).show().children("#msg").html("密码不能为空或者为空格!");
			return ;
		}*/
		if(!$(validBox).is(":hidden")) {
			if(validateCode.trim().length == 0) {
				$(showErrorMsg).show().children("#msg").html("验证码不能为空或者为空格!");
				return ;
			}
		}
		submitNow(account, password, validateCode, remember);
	}
	
	
	function submitNow(account, password, validateCode, remember) {
		var data = {"account": account, "password": password, "validCode": validateCode, "remember": remember} ;
		var clickEle = $(loginBtn);
		$.ajax({
			type : "POST",
			url : $.ctx+"/system/login/login.do",
			data : data,
			beforeSend: function() {
				keydown.unbind();
				loginClickBtn.unbind();
				$(loginBtn).html("<span class='fa fa-spinner fa-pulse'></span><span class='txt'>确认中...</span>");
			},
			success : function(result) {
				if (typeof result === "string" && !(result instanceof Array)){  
					result = $.parseJSON(result);
				}
				
				if(result.status) {
					$(loginBtn).html("<span class='fa fa-check'></span>登录成功");
					$(showErrorMsg).hide();
					//跳转进入主页
					setTimeout("window.location.replace('"+$.ctx+"/admin/index/adminIndex.do')", 500);
					
				} else{
					keydown.bind("keydown", loginEnter);
					loginClickBtn.bind("click", btnClick);
					$(showErrorMsg).show().children("#msg").html(result.msg);
					$(loginBtn).html("<span class='fa fa-key'></span><span class='txt'>登录</span>");
					//密码错误次数太多，显示验证码
					if(undefined != result.obj && result.obj.v) {
						if(result.obj.v) {
							if($(validBox).is(":hidden")) {
								$(validBox).show();
							}
							
							//需刷新验证码
							if(result.obj.v == true) {
								$(validateCodeImg).attr("src", $.ctx+"/validCodeServlet?d="+new Date().getTime());
							}
						}
					}
				}
				
			},
			complete: function () {},
		    error: function (data) {
		    	keydown.bind("keydown", loginEnter);
		    	loginClickBtn.bind("click", btnClick);
		    	$(loginBtn).html("<span class='fa fa-key'></span>登录");
		        alert("发生错误["+data.status+"]");
		    }
		}, 'JSON');
	}
	
	
	
	
	var iforgotClickBtn, iforgotBtnClick = null;
	window.loginpage.iforgotClick = function() {
		iforgotClickBtn = $(iforgotClick).bind("click", iforgotBtnClick = function() {
			iforgot();
		});
	}
	//找回密码
	iforgot = function() {
		var email = $("div.iforgot_box input[name=email]").val() ;
		var validateCode = $("div.iforgot_box input[name=validateCode]").val() ;
		
		$(showIforgotErrorMsg).css({"background": "#fff5f5", "border-color": "#fc4c24", "color": "red"});
		$("#msg-icon").removeClass("fa-check").addClass("fa-minus-circle");
		
		if(email.trim().length == 0) {
			$(showIforgotErrorMsg).show().children("#msg").html("邮箱地址不能为空!");
			return ;
		}
		if(validateCode.trim().length == 0) {
			$(showIforgotErrorMsg).show().children("#msg").html("验证码不能为空或者为空格!");
			return ;
		}
		
		var data = {"email": email, "validateCode": validateCode} ;
		$.ajax({
			type : "POST",
			url : $.ctx+"/system/login/iforgotPassword.do",
			data : data,
			beforeSend: function() {
				iforgotClickBtn.unbind();
				$(showIforgotErrorMsg).hide();
				$(iforgotClick).removeClass("btn-blue");
				
			},
			success : function(result) {
				if(result.status) {
					$(iforgotClick).children("span#count").html(60);
					countTime = setInterval(aa, 1000);
					$(showIforgotErrorMsg).show().css({"background": "#dff0d8", "border-color": "#d6e9c6", "color": "#3c763d"});
					$("#msg-icon").removeClass("fa-minus-circle").addClass("fa-check");
					$("span#msg").html("邮件已发送到您的邮箱...");
				} else {
					$("#iforgotValidateCodeImg").attr("src", $.ctx+"/validCodeServlet?d="+new Date().getTime());
					$(iforgotClick).addClass("btn-blue");
					$(showIforgotErrorMsg).show().children("#msg").html(result.msg);
					iforgotClickBtn.bind("click", iforgotBtnClick);
				}
			},
			complete: function () {},
		    error: function (data) {
		    	iforgotClickBtn.bind("click", iforgotBtnClick);
		    	$(iforgotClick).addClass("btn-blue");
		        alert("发生错误["+data.status+"]");
		    }
		}, 'JSON');
	};
	
	function aa() {
		var count = $("#count") ;
		var t = parseInt(count.text()) ;
		count.html((t-1));
		if(t == 1) {
			count.html("");
			window.clearInterval(countTime);
			iforgotClickBtn.bind("click", iforgotBtnClick);
			$(showIforgotErrorMsg).hide();
			$(iforgotClick).addClass("btn-blue");
		}
	}
	
	
	$(function(){
		window.loginpage.init();
		window.loginpage.clickLogin();
		window.loginpage.iforgotClick();
		window.loginpage.keydownLogin();
	});
})(jQuery);